#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPainter>
#include <QLabel>
#include "common.h"
#include "fileframe.h"
#include "view/filelistview.h"
#include "view/convertentryview.h"
#include "msgprint.h"

FileFrame::FileFrame(QWidget *parent) :
    QWidget(parent)
{
    setMinimumSize(500, 330);
    m_fileView = new FileListView(this);
    m_convertEntryView = new ConvertEntryView(this);
    connect(this, SIGNAL(signalLanguageChanged()), m_fileView, SIGNAL(signalLanguageChanged()));
    connect(this, SIGNAL(signalLanguageChanged()), m_convertEntryView, SIGNAL(signalLanguageChanged()));
    connect(m_fileView, SIGNAL(signalFileSelectionChanged(bool,bool)), m_convertEntryView, SLOT(slotFileSelectionChanged(bool,bool)));
    connect(m_fileView, SIGNAL(signalFileSelectionChanged(bool,bool)), this, SIGNAL(signalFileSelectionChanged(bool,bool)));
    initUI();
}

void FileFrame::initUI()
{
    // top bar
    QHBoxLayout *lt = new QHBoxLayout;
    QLabel *labeltl = createBglabel("tableview_bg_top_left.png");
    labeltl->setFixedSize(9, 13);
    QLabel *labeltm = createBglabel("tableview_bg_top_middle.png");
    labeltm->setFixedHeight(13);
    QLabel *labeltr = createBglabel("tableview_bg_top_right.png");
    labeltr->setFixedSize(7,13);
    lt->setSpacing(0);
    lt->setContentsMargins(0,0,0,0);
    lt->addWidget(labeltl);
    lt->addWidget(labeltm);
    lt->addWidget(labeltr);
    // middle
    QHBoxLayout *lm = new QHBoxLayout;
    QLabel *labelml = createBglabel("tableview_bg_middle_left.png");
    labelml->setFixedWidth(4);
    QVBoxLayout *layoutmm = new QVBoxLayout;
    layoutmm->setSpacing(0);
    layoutmm->setContentsMargins(0,0,0,0);
    layoutmm->addWidget(m_fileView);
    layoutmm->addWidget(m_convertEntryView);
    QLabel *labelmr = createBglabel("tableview_bg_middle_right.png");
    labelmr->setFixedWidth(3);
    lm->addWidget(labelml);
    lm->addLayout(layoutmm);
    lm->addWidget(labelmr);
    lm->setSpacing(0);
    lm->setContentsMargins(0,0,0,0);
    // bottom bar
    QHBoxLayout *lb = new QHBoxLayout;
    QLabel *labelbl = createBglabel("tableview_bg_bottom_left.png");
    labelbl->setFixedSize(9, 9);
    QLabel *labelbm = createBglabel("tableview_bg_bottom_middle.png");
    labelbm->setFixedHeight(9);
    QLabel *labelbr = createBglabel("tableview_bg_bottom_right.png");
    labelbr->setFixedSize(7,9);
    lb->addWidget(labelbl);
    lb->addWidget(labelbm);
    lb->addWidget(labelbr);
    lb->setSpacing(0);
    lb->setContentsMargins(0,0,0,0);
    // layout
    QVBoxLayout *lo = new QVBoxLayout;
    lo->addLayout(lt);
    lo->addLayout(lm);
    lo->addLayout(lb);
    lo->setSpacing(0);
    lo->setContentsMargins(0,0,0,0);
    this->setLayout(lo);
}

QLabel *FileFrame::createBglabel(const QString &imgpath)
{
    QLabel *label = new QLabel(this);
    label->setScaledContents(true);
    label->setPixmap(QPixmap(IMG_DIR+imgpath));
    return label;
}

void FileFrame::addFileList(const QStringList &filelist)
{
    m_fileView->addFileList(filelist);
}

void FileFrame::addDirectory(const QString &path)
{
    m_fileView->addDirectory(path);
}

void FileFrame::deleteSelectedFiles()
{
    m_fileView->deleteSelectedFiles();
}

/*
void FileFrame::addFile(const QString &filename)
{
    QTreeWidgetItem *item = new QTreeWidgetItem(m_fileView);

    QString baseFileName = QFileInfo(filename).fileName();

    item->setText(0, baseFileName);
    item->setFlags(item->flags() & ~Qt::ItemIsEditable);
    item->setTextAlignment(1, Qt::AlignCenter);

}

//////////////////////////////////////////////////////
//class fileView
//////////////////////////////////////////////////////
fileView::fileView(QWidget *parent)
    : QTreeWidget(parent)
{
    setAcceptDrops(true);
}

void fileView::dragMoveEvent(QDragMoveEvent *event)
{
    // Accept file actions with a '.torrent' extension.
    QUrl url(event->mimeData()->text());
    if (url.isValid() && url.scheme().toLower() == "file"
            && url.path().toLower().endsWith(".torrent"))
        event->acceptProposedAction();
}

void fileView::dropEvent(QDropEvent *event)
{
    // Accept drops if the file has a '.torrent' extension and it
    // exists.
    QString fileName = QUrl(event->mimeData()->text()).path();
    if (QFile::exists(fileName) && fileName.toLower().endsWith(".torrent"))
        emit fileDropped(fileName);
}
*/
